const fs = require("fs");
const path = require("path");
const routes = require("./router/index");
const config_build = require("./configs/build/index");

// 更新app.setting.js
console.log(config_build.appSetting);
const folderPath = './static';
const filePath = path.join(folderPath, 'app.setting.js');
if (!fs.existsSync(folderPath)) {
  fs.mkdirSync(folderPath);
}
fs.writeFile(
  filePath,
  `window.appSetting=${JSON.stringify(config_build.appSetting)}`,
  (err) => {
    if (err) {
      throw new Error("appSetting 写入失败");
    } else {
      console.log("appSetting 写入成功");
    }
  }
);

module.exports = {
  env: {
    MODE: process.env.MODE,
    APPCODE: config_build.appCode,
  },
  wrpSgHostUrl: config_build.wrpSgHostUrl,
  head: {
    titleTemplate: "%s",
    meta: [
      { charset: "utf-8" },
      {
        name: "viewport",
        content:
          "width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover",
      },
      { "http-equiv": "x-dns-prefetch-control", content: "on" },
      { hid: "description", name: "description", content: "Nuxt.js project" },
    ],
    link: [
      { rel: "icon", type: "image/x-icon", href: "/teld.ico" },
      { rel: "dns-prefetch", href: "//sgi.teld.cn" },
      {
        rel: "alternate",
        media: "only screen and (max-width: 640px)",
        href: "//web.teld.cn",
      },
    ],
    script: [
      { src: "/app.setting.js", defer: false },
      { src: "//res.wx.qq.com/open/js/jweixin-1.4.0.js", defer: true },
      // {
      //   src:
      //     "https://webapi.amap.com/maps?v=1.4.15&key=deeb12d16e6709c61751e5c008ba11ed&plugin=AMap.Geolocation",
      //   defer: true,
      // },
      // 可加装的容器url
      // TeldApp_JSSDK_Url : "//cv.teld.cn/static/original/js/jteld.js?v=2.0",
      // WX_JSSDK_Url : "//res.wx.qq.com/open/js/jweixin-1.4.0.js",
      // Alipay_JSSDK_Url : "//gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.min.js",
      // GaoDeMapUrl:"https://webapi.amap.com/maps?v=1.4.15&key=deeb12d16e6709c61751e5c008ba11ed&plugin=AMap.ToolBar&callback=MapInit",
      // UseGaoDeMapUIURL:"https://webapi.amap.com/ui/1.0/main.js?v=1.0.11",
    ],
  },
  css: [
    // "~/assets/custom-pc.scss",
    "~/assets/custom-mobile.less",
    "~/assets/tailwindcss.css",
  ],
  plugins: [
    //需要pc端组件时引入，不需要的场合，为了性能，请注释
    // {
    //   src: "@@/plugins/components/pc",
    //   ssr: true,
    // },
    //需要移动端组件时引入，不需要的场合，为了性能，请注释
    {
      src: "@@/plugins/components/mobile",
      ssr: true,
    },
    {
      src: "~/plugins/store",
      ssr: false,
    },
    {
      src: "~/plugins/teld-plugins",
      ssr: false,
    },
    {
      src: "~/plugins/directives",
      ssr: false,
    },
    {
      src: "~/plugins/filters",
      ssr: false,
    },
    {
      src: "~/plugins/router",
      ssr: false,
    }
  ],
  build: {
    transpile: ["axios", "teld-web-component", /vant.*?less/],
    analyze: false,
    filenames: {
      vendor: "[name].js",
      app: "[name].js",
      manifest: "manifest.js",
    },
    uglify: true,
    modules: ["@nuxtjs/style-resources"],
    styleResources: {
      less: "./assets/vars.less",
    },
    babel: {
      plugins: [
        [
          "import",
          {
            libraryName: "vant",
            style: true,
          },
          "vant",
        ],
      ],
    },
    extend(config) {
      config.module.rules.push({
        test: /\.less$/,
        use: [
          {
            loader: "less-loader",
            options: {
              modifyVars: {
                hack: `true; @import "${path.resolve("./assets/vars.less")}";`,
              },
            },
          },
        ],
      });
    },
    postcss: {
      plugins: {
        tailwindcss: {},
        "postcss-rem-to-pixel": { rootValue: 14, propList: ['*'] },
        "postcss-px-to-viewport": { viewportWidth: 375 },
        // "postcss-pxtorem": {
        //   rootValue: 14,
        // },
        autoprefixer: {},
      },
    },
  },

  router: {
    extendRoutes(router) {
      router.push(...routes);
    },
    // scrollBehavior: (to, from, savedPosition) => {
    //   let position = savedPosition;

    //   return new Promise((resolve) => {
    //     window.$nuxt.$once("triggerScroll", () => {
    //       try {
    //         if (!position) {
    //           position = JSON.parse(
    //             window.sessionStorage.getItem("scrollPosition")
    //           );
    //         }
    //         resolve(position);
    //       } catch (error) {
    //         resolve();
    //       }
    //     });
    //   });
    // },
  },
};
